import aiohttp
import asyncio

from requests import request
from jinja_handler import create_message

# import requests


class Web_app:
    def __init__(self) -> None:
        self.obj_queue_fifo_read = asyncio.Queue()
        self.obj_queue_fifo_write = asyncio.Queue()
        self.list_response = []
        self.d = {
            "request": "post...get...put",
            "url": "http:kyni.ru",
            "data": {
                "headers": {
                    "Authorization": "Bearer " + "__token",
                    "content-type": "application/json",
                },
                "path_name": ["path/file/create", "web1_jinja.html"],
            },
            "error": {404: ["path/file/create", "web1_jinja.html"]},
        }

    async def client_session_while(self):
        if self.obj_queue_fifo_read.empty():
            dict_data = await self.obj_queue_fifo_read.get()
            type_http_request = dict_data["request"]
            url = dict_data["url"]
            data = dict_data["data"]
            typle_path_name = dict_data["path_name"]
            error = dict_data["error"]
            if type_http_request == "get":
                await self.request_get(url, typle_path_name, error)
            elif type_http_request == "post":
                await self.request_post(url, typle_path_name, data, error)
            elif type_http_request == "put":
                await self.request_put(url, typle_path_name, data, error)
            else:
                pass
        else:
            pass

    async def request_get(self, url, typle_path_name, error):
        async with aiohttp.ClientSession(url) as session:
            async with session.get("/get") as response:
                response_status = response.status
                if response_status is self.list_response:
                    html = await response.json()
                    path = typle_path_name[0]
                    name = typle_path_name[1]
                    mess = await create_message(path, name, html)
                    await self.obj_queue_fifo_write.put(mess)
                else:
                    path_error = error[response_status][0]
                    name_error = error[response_status][1]
                    mess = await create_message(path_error, name_error)
                    await self.obj_queue_fifo_write.put(mess)

    # async with session.get('http://httpbin.org/get',
    #                   params='key=value+1') as r:
    # async with session.post(url, json={'test': 'object'})
    # await session.post(url,
    #               data=payload,
    #               headers=headers)
    async def request_post(self, url, typle_path_name, data, error):
        async with aiohttp.ClientSession(url) as session:
            user_data = data["data"]
            user_params = data["params"]
            user_headers = data["headers"]
            async with session.post("/post", data=data) as response:
                response_status = response.status
                if response_status is self.list_response:
                    html = await response.json()
                    path = typle_path_name[0]
                    name = typle_path_name[1]
                    mess = await create_message(path, name, html)
                    await self.obj_queue_fifo_write.put(mess)
                else:
                    path_error = error[response_status][0]
                    name_error = error[response_status][1]
                    mess = await create_message(path_error, name_error)
                    await self.obj_queue_fifo_write.put(mess)

    async def request_put(self, url, typle_path_name, data, error):
        async with aiohttp.ClientSession(url) as session:
            async with session.put("/put", data=data) as response:
                response_status = response.status
                if response_status is self.list_response:
                    html = await response.json()
                    path = typle_path_name[0]
                    name = typle_path_name[1]
                    mess = await create_message(path, name, html)
                    await self.obj_queue_fifo_write.put(mess)
                else:
                    path_error = error[response_status][0]
                    name_error = error[response_status][1]
                    mess = await create_message(path_error, name_error)
                    await self.obj_queue_fifo_write.put(mess)

    def main(self):
        loop = asyncio.get_event_loop()
        asyncio.ensure_future(self.client_session_while())
        loop.run_forever()
